in progress/rotations2.R

# three by three matrix.

# The square can be changed by rotating or by mirroring, or both.
# 

# walks that start at 1 have 
walks=games::bogglewalks[["9"]] # 784 walks

walks[walks[,1]==1 & walks[,2]==2 | 
   walks[,1]==1 & walks[,2]==5 & walks[,3]==2 | 
   walks[,1]==1 & walks[,2]==5 & walks[,3]==9 & walks[,4]==6 | 
   walks[,2]==2 | walks[,2]==5, ]
      

T0=c(1,2,3, 4,5,6, 7,8,9) # default, R1, R2, R3
T1=c(3,6,9, 2,5,8, 1,4,7) # rotation 1, C1, C2, C3
T2=c(9,8,7, 6,5,4, 3,2,1) # rotation 2, R3-r, R2-r, R1-r
T3=c(7,4,1, 8,5,2, 9,6,3) # rotation 3, C1-r, C2-r, C3-r
T4=c(1,4,7, 2,5,8, 3,6,9) # diagonal top left, C1, C2, C3
T5=c(9,6,3, 8,5,2, 7,4,1) # diagonal top right, C3-r, C2-r, C1-r
T6=c(7,8,9, 4,5,6, 1,2,3) # mirror horizontal, R3, R2, R1
T7=c(3,2,1, 6,5,4, 9,8,7) # mirror vertical, R1-r, R2-r, R3-r

# write a function that does this for square of any size.
# Numbers inside a row/column need to be kept together, but 
# their order or their orientation may be reversed.

# This means that the number of bogglewalks is always a multipication of 8.
# Argument is the number of elements in the square. It must have a square root.
mirrormatrix=function(square=9){
   stopifnot(sqrt(square)==round(sqrt(square)))
   list(m0=matrix(1:square,byrow=TRUE,ncol=sqrt(square)), # zero
        m1=matrix(1:square,byrow=FALSE,ncol=sqrt(square)), # diagonal topleft
        m2=matrix(square:1,byrow=TRUE,ncol=sqrt(square)), # rotation 2
        m3=matrix(square:1,byrow=FALSE,ncol=sqrt(square)), # diagonal topright
        m4=matrix(sqrt(square)*((((square-1):0))%/%sqrt(square))+(1+(0:(square-1))%%sqrt(square)),byrow=T,ncol=sqrt(square)),
        m5=matrix(sqrt(square)*((((square-1):0))%/%sqrt(square))+(1+(0:(square-1))%%sqrt(square)),byrow=F,ncol=sqrt(square)), # 
        m6=matrix(sqrt(square)*(((0:(square-1)))%/%sqrt(square))+(1+((square-1):0)%%sqrt(square)),byrow=T,ncol=sqrt(square)), #
        m7=matrix(sqrt(square)*(((0:(square-1)))%/%sqrt(square))+(1+((square-1):0)%%sqrt(square)),byrow=F,ncol=sqrt(square)))} # 

# %/% integer division; %% rest division
# create: 
# 7,8,9, 4,5,6, 1,2,3
# 9,8,7, 6,5,4, 3,2,1
3*(tmp[9:1]-1)%/%3 + (1+(tmp-1)%%3)
square=9
matrix(sqrt(square)*(((0:(square-1)))%/%sqrt(square))+(1+((square-1):0)%%sqrt(square)),byrow=T,ncol=sqrt(square))


word=c("a","b","c","d","e","f","g","h","i")
rotate=function(word,rotation){matrix(strsplit(word,split="")[[1]][rotation],byrow=T,ncol=3)}
rotate(word,T0)

basic9=rbind(
   t(apply(walks[walks[,1]==1,],1,function(x){T0[x]})),
   t(apply(walks[walks[,1]==3,],1,function(x){T1[x]})),
   t(apply(walks[walks[,1]==9,],1,function(x){T2[x]})),
   t(apply(walks[walks[,1]==7,],1,function(x){T3[x]})),
   t(apply(walks[walks[,1]==1 & walks[,2]==2,],1,function(x){T4[x]})))

tmp=rbind(t(apply(walks[walks[,1]==1 & walks[,2]==2,],1,function(x){T4[x]})),
   walks[walks[,1]==1 & walks[,2]==4,])

# There are three unique starting points. Corner, middle, center.

# Four x Four matrix.
# There are three unique starting points: corner, one next to the corner, and one of
# the four cells of the "inner" (2x2) square. For the outer square there are four rotations and
# 2 mirror images. For the inner square, there are four rotations. But they cannot rotate
# independently of one another.

walks=bogglewalks[["16"]] # 201125: 331K different walks

#
table(walks[walks[,1]%in%c(1,4,16,13),1]) # corners: 36K
table(walks[walks[,1]%in%c(2,3,8,12,14,15,5,9),1])  # outer square: 17K
table(walks[walks[,1]%in%c(6,7,11,10),1]) # inner square: 12K 

# Rotations (also as a dataset in the games library).
T0=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) # no rotation
T1=c(13,9,5,1,14,10,6,2,15,11,7,3,16,12,8,4) #
T2=c(16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
T3=c(4,8,12,16,3,7,11,15,2,6,10,14,1,5,9,13)
T4=c(4,3,2,1,8,7,6,5,12,11,10,9,16,15,14,13)
T5=c(1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16) # diagonal
T6=c(13,14,15,16,9,10,11,12,5,6,7,8,1,2,3,4)
T7=c(16,12,8,4,15,11,7,3,14,10,6,2,13,9,5,1)

TD=c(1,5,9,

# Transforming the walks that can be obtained by rotating the square:
basicwalks=rbind(
t(apply(walks[walks[,1]==1,],1,function(x){T0[x]})),
t(apply(walks[walks[,1]==4,],1,function(x){T1[x]})),
t(apply(walks[walks[,1]==16,],1,function(x){T2[x]})),
t(apply(walks[walks[,1]==13,],1,function(x){T3[x]})),
t(apply(walks[walks[,1]==2,],1,function(x){T0[x]})),
t(apply(walks[walks[,1]==8,],1,function(x){T1[x]})),
t(apply(walks[walks[,1]==15,],1,function(x){T2[x]})),
t(apply(walks[walks[,1]==9,],1,function(x){T3[x]})),
t(apply(walks[walks[,1]==3,],1,function(x){T4[x]})),
t(apply(walks[walks[,1]==5,],1,function(x){T5[x]})),
t(apply(walks[walks[,1]==14,],1,function(x){T6[x]})),
t(apply(walks[walks[,1]==12,],1,function(x){T7[x]})),
t(apply(walks[walks[,1]==6,],1,function(x){T0[x]})),
t(apply(walks[walks[,1]==7,],1,function(x){T1[x]})),
t(apply(walks[walks[,1]==11,],1,function(x){T2[x]})),
t(apply(walks[walks[,1]==10,],1,function(x){T3[x]})))

basicwalks=basicwalks[!duplicated(basicwalks),]
table(basicwalks[,1]) # only values are 1, 2 and 6, 37K, 17K and 12K respectively. Total 68K rows.

t(apply(basicwalks[basicwalks[,1]==1 & basicwalks[,2]==5,],1,function(x){T5[x]}))
tmp=rbind(t(apply(basicwalks[basicwalks[,1]==1 & basicwalks[,2]==5,],1,function(x){T5[x]})),
   basicwalks[basicwalks[,1]==1 & basicwalks[,2]==2,])

# Next step is to see if we can still find new walks from any of these three
# starting points.
vdweijer/games documentation built on Dec. 23, 2021, 3:02 p.m.